package com.greenant.store.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SaveCallback;
import com.greenant.store.R;
import com.greenant.store.utils.UtilTools;
import com.greenant.store.utils.iLog;
import com.lsarah.utils.SystemBarTintManager;

public class MyInfoActivity extends BaseActivity {

	private EditText name;
	// private EditText mobile;
	// private EditText email;
	private EditText idnumber;
	private TextView modify;
	private TextView iv_title;
	private EditText info_phone;
	private TextView info_type;

	private Context mContext;

	private final static String REALNAME = "realname";
	private final static String IDNUMBER = "IDNumber";
	private final static String MOBILEPHONENUMBER = "mobilePhoneNumber";
	private final static String USERSTYLE = "userStyle";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;

		SystemBarTintManager.setContentView(this, R.layout.activity_myinfo,
				getResources().getColor(R.color.firebrick));
		name = (EditText) this.findViewById(R.id.name);

		idnumber = (EditText) this.findViewById(R.id.info_idnumber);
		modify = (TextView) this.findViewById(R.id.modify);
		info_phone = (EditText) this.findViewById(R.id.info_phone);
		info_type = (TextView) this.findViewById(R.id.info_type);

		final AVUser current = AVUser.getCurrentUser();
		name.setText(current.getString(REALNAME));
		String phoneNum = current.getString(MOBILEPHONENUMBER);

		if (!TextUtils.isEmpty(phoneNum) && phoneNum.length() > 7) {

			phoneNum = phoneNum.substring(0, 3) + "****"
					+ phoneNum.substring(7);

			info_phone.setText(phoneNum);
		}
		info_phone.setEnabled(false);
		String userStyle = current.getString(USERSTYLE);

		// CVS,SP,CUSTOM
		if ("CVS".equalsIgnoreCase(userStyle)
				|| "SP".equalsIgnoreCase(userStyle)) {

			info_type.setText(R.string.info_type_store);
		} else {
			info_type.setText(R.string.info_type_person);
		}

		idnumber.setText(current.getString(IDNUMBER));

		modify.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!checkIfIsCorrect()) {
					UtilTools.showMyToast(mContext,
							mContext.getString(R.string.failed));
					return;
				}
				current.put(REALNAME, name.getText().toString());
				current.put(IDNUMBER, idnumber.getText().toString());
				current.saveInBackground(new SaveCallback() {
					@Override
					public void done(AVException arg0) {
						if (arg0 == null) {
							UtilTools.showMyToast(mContext,
									mContext.getString(R.string.success));
						} else {
							int code = arg0.getCode();
							if (code == 125) {
								UtilTools.showMyToast(
										mContext,
										mContext.getString(R.string.error_register_email_invalid));
							} else if (code == 203) {
								UtilTools.showMyToast(
										mContext,
										mContext.getString(R.string.error_register_email_repeat));
							} else {
								UtilTools.showMyToast(mContext,
										mContext.getString(R.string.failed));
							}
							iLog.d("getLocalizedMessage:"
									+ arg0.getLocalizedMessage() + " "
									+ arg0.getMessage());
						}
					}
				});
			}
		});
	}

	public void goBack(View view) {
		finish();
	}

	public void addressManage(View view) {
		// Intent intent = new Intent(this, MyAddressActivity.class);
		// startActivity(intent);
	}

	private boolean checkIfIsCorrect() {
		if (TextUtils.isEmpty(name.getText().toString())
				|| TextUtils.isEmpty(idnumber.getText().toString())) {
			return false;
		}
		return true;
	}

}
